﻿using MCUPackage;
using MCUPackage.DesignEditor;
using MCUPackage.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

namespace SxPackages.STC32G.Models
{
	[Serializable]
	class AllPWMOptions
	{
		/// <summary>
		/// PWM通道设置集合
		/// </summary>
		[Browsable(false),
		JsonIgnore]
		public PWMOptionBase[] All { get; private set; }

		[Category("PWMA"),
		DisplayName("PWMA组配置"),
		TypeConverter(typeof(PropertySorter))]
		public PWMGrpOptionBase PWMA { get; private set; }

		[Category("PWMA"),
		DisplayName("PWM1通道配置"),
		TypeConverter(typeof(PropertySorter))]
		public PWM1Options PWM1 { get; private set; }

		[Category("PWMA"),
		DisplayName("PWM2通道配置"),
		TypeConverter(typeof(PropertySorter))]
		public PWM2Options PWM2 { get; private set; }

		[Category("PWMA"),
		DisplayName("PWM3通道配置"),
		TypeConverter(typeof(PropertySorter))]
		public PWM3Options PWM3 { get; private set; }

		[Category("PWMA"),
		DisplayName("PWM4通道配置"),
		TypeConverter(typeof(PropertySorter))]
		public PWM4Options PWM4 { get; private set; }

		[Category("PWMB"),
		DisplayName("PWMB组配置"),
		TypeConverter(typeof(PropertySorter))]
		public PWMBOptions PWMB { get; private set; }

		[Category("PWMB"),
		DisplayName("PWM5通道配置"),
		TypeConverter(typeof(PropertySorter))]
		public PWM5Options PWM5 { get; private set; }

		[Category("PWMB"),
		DisplayName("PWM6通道配置"),
		TypeConverter(typeof(PropertySorter))]
		public PWM6Options PWM6 { get; private set; }

		[Category("PWMB"),
		DisplayName("PWM7通道配置"),
		TypeConverter(typeof(PropertySorter))]
		public PWM7Options PWM7 { get; private set; }

		[Category("PWMB"),
		DisplayName("PWM8通道配置"),
		TypeConverter(typeof(PropertySorter))]
		public PWM8Options PWM8 { get; private set; }

		public AllPWMOptions(STC32GQFP64 aParent)
		{
			this.PWMA = new PWMGrpOptionBase(aParent);
			this.PWM1 = new PWM1Options(aParent);
			this.PWM2 = new PWM2Options(aParent);
			this.PWM3 = new PWM3Options(aParent);
			this.PWM4 = new PWM4Options(aParent);

			this.PWMB = new PWMBOptions(aParent);
			this.PWM5 = new PWM5Options(aParent);
			this.PWM6 = new PWM6Options(aParent);
			this.PWM7 = new PWM7Options(aParent);
			this.PWM8 = new PWM8Options(aParent);
			All = new PWMOptionBase[] { this.PWM1, this.PWM2, this.PWM3, this.PWM4, this.PWM5, this.PWM6, this.PWM7, this.PWM8 };
		}

		/// <summary>
		/// 返回是否有使能的PWM通道
		/// </summary>
		/// <returns></returns>
		public bool HasEnabled()
		{
			foreach (var pwm in All)
			{
				if (pwm.Enable)
				{
					return true;
				}
			}

			return false;
		}

		public bool AppendConfig(List<String[]> aLst)
		{
			if (!HasEnabled())
			{
				return false;
			}

			string name = "PWM_Config";
			StringBuilder funcSb = new StringBuilder();
			funcSb.Append(TemplateHelper.GetFunctionCommit("PWM模块初始化"));
			funcSb.AppendLine($"PRIVATE void {name}(void)").AppendLine("{");
			funcSb.AppendLine("\tPWMx_InitDefine attr;");

			Dictionary<string, List<string>> lst = new Dictionary<string, List<string>>();
			StringBuilder initSb = new StringBuilder();
			StringBuilder tmpSb = new StringBuilder();
			// PWMA组数据
			for (int i = 0; i < 4; i++)
			{
				string str = this.All[i].Generate(lst);
				if (!string.IsNullOrEmpty(str))
				{
					initSb.AppendLine().AppendLine(str);
				}
			}
			if (initSb.Length > 0)
			{
				// 有初始化数据
				tmpSb.Append(initSb.ToString()).AppendLine();
				tmpSb.AppendLine(this.PWMA.Generate());
				initSb.Clear();
			}

			// PWMB组数据
			for (int i = 4; i < 8; i++)
			{
				string str = this.All[i].Generate(lst);
				if (!string.IsNullOrEmpty(str))
				{
					initSb.AppendLine().AppendLine(str);
				}
			}
			if (initSb.Length > 0)
			{
				// 有初始化数据
				tmpSb.Append(initSb.ToString()).AppendLine();
				tmpSb.AppendLine(this.PWMB.Generate());
				initSb.Clear();
			}

			if (lst.Count > 0)
			{
				funcSb.AppendLine();
				funcSb.Append(GPIOOptions.GetGPIOConfigs(lst));
			}

			funcSb.Append(tmpSb.ToString());
			funcSb.AppendLine("}");
			aLst.Add(new string[] { funcSb.ToString(), $"{name}();" });
			return true;
		}
	}
}
